/*
 * @Copyright: 2025 Copyright (C) AMCAX, All rights reserved.
 * @Author: Zhang
 * @Date: 2025/7/24 19:39
 * @LastEditTime: 2025/7/24 19:39
 * @LastEditors: Zhang
 * @Description:
 */
import { useDocumentStore } from '@/common/store';
import { modelLayout } from '@/domain';
import { CmdDialogForm } from 'nextcae-base';
import { useState } from 'react';
import { NMMeshBaseCommand, TopoDialogProps } from '../type';
import { schema, uiSchema } from './schema';
import { TwoPointPlaneField } from './TwoPointPlaneField';

const JoinProjectionDialog = ({ title, onClose }: TopoDialogProps) => {
  const { workingObject } = useDocumentStore();
  const [formData, setFormData] = useState(null);

  const onSubmit = async (formData) => {
    const planeInfo = formData.Selection.Plane;
    const params = {
      root: workingObject,
      type: 'vertical',
      fidx: planeInfo.entities.map(({ idx }) => idx),
      points: planeInfo.entities.flatMap(({ point }) => point) as number[],
      normal: planeInfo.normals.flatMap((n) => n) as number[],
    };
    await modelLayout.createCurve(params);
    onClose();
  };
  return (
    <>
      <CmdDialogForm
        title={title}
        titleIcon={'commands/modelRouting/vertical-curve'}
        fields={{ twoPointPlaneField: TwoPointPlaneField }}
        schema={schema}
        extraUiSchema={uiSchema}
        formData={formData}
        onChange={(data) => {
          setFormData(data.formData);
        }}
        onSubmit={onSubmit}
        onClose={onClose}
        isFormDataValid={formData?.Selection?.Plane != null}
        width='300px'
      />
    </>
  );
};
export const cmdJoinProjection: NMMeshBaseCommand = {
  id: 'cmdJoinProjection',
  icon: 'commands/modelRouting/vertical-curve',
  label: '连线投影',
  create: (label: string, onClose: () => void, props) => (
    <JoinProjectionDialog
      title={label}
      onClose={onClose}
      props={props}
      placement='topLeft'
    />
  ),
};
